package com.dmap.knowledge.controller.background;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.knowledge.controller.api.param.BannerEditParam;
import com.dmap.knowledge.controller.api.param.BannerParam;
import com.dmap.knowledge.service.BannerExtensionService;
import com.dmap.knowledge.service.BannerService;
import com.dmap.knowledge.service.BannerTagService;
import com.dmap.knowledge.vo.BannerInfoOneVo;
import com.dmap.knowledge.vo.BannerInfoVo;
import com.dmap.knowledge.vo.BannerTagVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Api(value = "轮播图", description = "background/轮播图")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/banner")
public class BannerBackController {
    @Resource
    private BannerService bannerService;
    @Resource
    private BannerTagService bannerTagService;
    @Resource
    private BannerExtensionService bannerExtensionService;
    
    @Transactional(propagation = Propagation.REQUIRED)
    @ResponseBody
    @ApiOperation(value = "新增轮播图", notes = "新增轮播图")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertBanner(@RequestBody BannerParam bannerParam) {
        BaseVo result = new BaseVo();
        if (bannerParam.getLang() == null || bannerParam.getType() == 0 || bannerParam.getImageUrl() == null || bannerParam.getTitle() == null || bannerParam.getJumpUrl() == null || bannerParam.getEndTime() == 0 || bannerParam.getStartTime() == 0 || bannerParam.getTags() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(bannerParam.getLang()) || StringUtils.isBlank(bannerParam.getImageUrl()) || StringUtils.isBlank(bannerParam.getTitle()) || StringUtils.isBlank(bannerParam.getJumpUrl())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        long success = bannerService.insertBanner(bannerParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }
    
    @Transactional(propagation = Propagation.REQUIRED)
    @ResponseBody
    @ApiOperation(value = "修改轮播图", notes = "修改轮播图")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseVo updateBanner(@RequestBody(required = false) BannerEditParam bannerParam) {
        BaseVo result = new BaseVo();
        if (bannerParam.getBannerId() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(bannerParam.getBannerId())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = bannerService.updateBanner(bannerParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
            return result;
        }
        int deleteSuccess = bannerTagService.deleteBannerTagByBannerId(Long.parseLong(bannerParam.getBannerId()));
        if (deleteSuccess == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
            return result;
        }
        if (CollectionUtils.isNotEmpty(bannerParam.getTags())) {
            int count = 0;
            for (BannerTagVo bannerTag : bannerParam.getTags()) {
                count = count + bannerTagService.insertBannerTag(Long.parseLong(bannerParam.getBannerId()), bannerTag.getTagTypeId(), bannerTag.getTagTypeName(), bannerTag.getTagId(), bannerTag.getTagName(), System.currentTimeMillis() / 1000);
            }
            if (count != bannerParam.getTags().size()) {
                result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
                result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
                return result;
            }
        } else {
            return result;
        }
        return result;
    }
    
    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除轮播图", notes = "删除轮播图")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public BaseVo deleteBanner(@RequestParam String bannerIds) {
        BaseVo result = new BaseVo();
        if (bannerIds == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(bannerIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int deleteTagSuccess = bannerTagService.deleteBannerTagList(bannerIds);
        if (deleteTagSuccess == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
            return result;
            
        }
        int deleteSuccess = bannerService.deleteBannerList(bannerIds);
        if (deleteSuccess == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
            return result;
        }
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "查询轮播图", notes = "查询轮播图")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseVo selectBanner(@RequestParam(value = "appNumber", required = false) String appNumber,
                               @RequestParam String lang,
                               @RequestParam(required = false) String companyNumber,
                               @RequestParam(required = false, defaultValue = "0") long order,
                               @RequestParam(value = "key", required = false) String key,
                               @RequestParam int pageIndex,
                               @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
        if (lang == null || pageIndex == 0 || pageSize == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appNumber != null && StringUtils.isBlank(appNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (companyNumber != null && StringUtils.isBlank(companyNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (key != null && StringUtils.isBlank(key)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        BannerInfoVo bannerList = bannerExtensionService.selectBannerList(appNumber, lang, companyNumber, key, pageIndex, pageSize);
        result.setData(bannerList);
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "查询单个轮播图", notes = "查询单个轮播图")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseVo selectBannerInfo(@RequestParam String bannerId) {
        BaseVo result = new BaseVo();
        if (bannerId == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(bannerId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        BannerInfoOneVo bannerList = bannerExtensionService.selectBannerInfo(bannerId);
        if (bannerList == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        result.setData(bannerList);
        return result;
    }
}
